package com.icloud.article.boot.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.icloud.article.boot.dto.AdminArticlePageDto;
import com.icloud.article.boot.service.ArticleService;
import com.icloud.article.boot.vo.AdminArticleVo;
import com.icloud.article.boot.vo.ArticleVo;
import com.icloud.common.core.api.CommonResult;
import com.icloud.common.database.enums.TFEnum;
import com.icloud.common.database.model.Article;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "后台帖子接口")
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminArticleController {

    private final ArticleService articleService;

    @ApiOperation("分页")
    @PostMapping("/page")
    public CommonResult<IPage<AdminArticleVo>> page(@Validated @RequestBody AdminArticlePageDto dto){
        return CommonResult.success(articleService.adminPage(dto));
    }

    @ApiOperation("详情")
    @GetMapping("/load")
    public CommonResult<ArticleVo> load(@ApiParam("帖子id") @RequestParam("id") Long id){
        return CommonResult.success(articleService.load(id));
    }

    @ApiOperation("开启风控")
    @PutMapping("/riskctl/open/{id}")
    //@Permission("article.edit")
    public CommonResult<Boolean> banned(@ApiParam("帖子id") @PathVariable("id") Long id){
        boolean result = articleService.update(new LambdaUpdateWrapper<Article>().eq(Article::getId,id).set(Article::getRiskCtl, TFEnum.TRUE.getCode()));
        return CommonResult.success(result);
    }

    @ApiOperation("关闭风控")
    @PutMapping("/riskctl/close/{id}")
    //@Permission("article.edit")
    public CommonResult<Boolean> cancelBanned(@ApiParam("帖子id") @PathVariable("id") Long id){
        boolean result = articleService.update(new LambdaUpdateWrapper<Article>().eq(Article::getId,id).set(Article::getRiskCtl, TFEnum.FALSE.getCode()));
        return CommonResult.success(result);
    }


    @ApiOperation("删除帖子")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Boolean> delete(@ApiParam("帖子id") @PathVariable("id") Long id){
        boolean result = articleService.delete(id);
        return CommonResult.success(result);
    }
}
